fix(useInterval): prevent immediate callback from refiring when enabled is toggled #352
Open
eunwoo-levi wants to merge 5 commits intotoss:mainfrom
Open
fix(useInterval): prevent immediate callback from refiring when enabled is toggled #352eunwoo-levi wants to merge 5 commits intotoss:mainfrom
eunwoo-levi wants to merge 5 commits intotoss:mainfrom
Conversation
…ed is toggled The immediate effect depended on `enabled`, so the callback fired again every time `enabled` went from false to true. Added `immediateCalledRef` to ensure the callback is called only once per interval lifecycle. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…abled toggle Covers the missing case where immediate=true and enabled is toggled false → true after mount — the callback should fire only once. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Fixes useInterval’s immediate: true behavior so the callback does not run again when enabled is toggled from false back to true.
Changes:
- Add an
immediateCalledRefguard so the “immediate” callback runs only once. - Add a regression test covering
enabledtoggling withimmediate: true. - Add a patch changeset for release notes/versioning.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| packages/core/src/hooks/useInterval/useInterval.ts | Adds a ref-based guard to prevent immediate callback re-firing on enabled toggles. |
| packages/core/src/hooks/useInterval/useInterval.spec.ts | Adds a test ensuring immediate callback does not re-fire after disabling/re-enabling. |
| .changeset/lovely-spies-change.md | Declares a patch release note for the fix. |
Comment on lines
+52
to
+60
| if (immediate !== true || !enabled) { | ||
| return; | ||
| } | ||
|
|
||
| if (immediateCalledRef.current) { | ||
| return; | ||
| } | ||
|
|
||
| immediateCalledRef.current = true; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
useIntervalwithimmediate: truere-fires the callback every timeenabledgoes fromfalsetotrue, even though it should only fire once when the interval first starts.Root Cause
enabledis included in the immediate effect's dependency array, so the effect re-runs on everyenabledchange.Fix
Added immediateCalledRef to ensure the callback fires only once per interval lifecycle.
Checklist
yarn run fixto format and lint the code and docs?yarn run test:coverageto make sure there is no uncovered line?